import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import {nanoid} from 'nanoid'

Vue.use(Vuex)

const actions = {
    addSaying(miniStore){
        axios.get('https://api.uixsj.cn/hitokoto/get?type=soup').then(
            response => {
                miniStore.commit('ADD_SAYING',response.data)
            },
            error => {
                alert('请求错误...!!!')
            })
    }
}

const mutations = {
    ADD_SCHOOL_AGE(state,value) {
        state.school.age += value
    },
    ADD_SAYING(state,value){
        const sayingObj = {id:nanoid(),title:value}
		state.saying.unshift(sayingObj)
    }
}

const state = {
    school:{
        name: '尚硅谷',
        address: '宏福科技园',
        age: 8
    },
    saying:[
        {id:'001',title: '君不见，黄河之水天上来，奔流到海不复回' ,author:'李白'}
    ]
}

const getters = {}

export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})